package com.sangfor.ssl.service.setting;

import android.os.Build;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.IVpnDelegate;
import com.sangfor.ssl.rdp.utils.crypto.Crypto;
import com.sangfor.ssl.service.utils.Common;
import com.sangfor.ssl.service.utils.IGeneral;
import com.sangfor.ssl.service.utils.xml.XmlReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* loaded from: classes2.dex */
public class SystemConfiguration {
    private static final int CLIPBOARD_ENABLED_FLAG = 1;
    private static final int CLIPBOARD_INCOMING_FLAG = 2;
    private static final int CLIPBOARD_OUTGOING_FLAG = 4;
    public static final String CONFIG_EFLAG_CLOSE = "close";
    public static final int CONFIG_TYPE_CONF = 3;
    public static final String CONFIG_TYPE_EFLAG = "EFlag";
    public static final int CONFIG_TYPE_LONGINAUTH = 2;
    public static final String CONFIG_TYPE_MESSAGE_DIGEST = "Extra";
    public static final int CONFIG_TYPE_PHONEMODULE = 1;
    public static String OFF = "0";
    public static String ON = "1";
    private static final String TAG = "SystemConfiguration";
    private static final String TICKET_ENABLE = "TicketEnable";
    private static final String TICKET_LOGIN_CODE = "TicketLoginCode";
    private static SystemConfiguration instance = null;
    public static boolean isAbleWork = true;
    private int mVersionType;
    private Map<String, String> mTicketMap = new HashMap();
    private HashMap<String, Object> authMap = new HashMap<>();
    private HashMap<String, Object> confMap = new HashMap<>();
    private HashMap<String, Object> moduleMap = new HashMap<>();
    private HashMap<String, Integer> versionIntMap = new HashMap<>();

    public boolean isAclEnable() {
        return false;
    }

    public boolean isIsolationEnable() {
        return false;
    }

    public boolean sslProtocolSupported() {
        return true;
    }

    private SystemConfiguration() {
    }

    public static synchronized SystemConfiguration getInstance() {
        SystemConfiguration systemConfiguration;
        synchronized (SystemConfiguration.class) {
            if (instance == null) {
                instance = new SystemConfiguration();
            }
            systemConfiguration = instance;
        }
        return systemConfiguration;
    }

    public static synchronized void destoryObject() {
        synchronized (SystemConfiguration.class) {
            instance = new SystemConfiguration();
        }
    }

    private HashMap<String, Object> buildModuleMap(String str) {
        Map<String, Object> parse;
        HashMap hashMap;
        ArrayList arrayList;
        if (str == null || str.equals("") || (parse = XmlReader.parse(str)) == null || (hashMap = (HashMap) parse.get("Modules")) == null || (arrayList = (ArrayList) hashMap.get("Module")) == null) {
            return null;
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            HashMap<String, Object> hashMap2 = (HashMap) it.next();
            if ("Android Phone".equals((String) hashMap2.get("ModuleName"))) {
                return hashMap2;
            }
        }
        return null;
    }

    private HashMap<String, Object> buildComMap(String str, String str2) {
        Map<String, Object> parse;
        if (str == null || str.equals("") || (parse = XmlReader.parse(str)) == null) {
            return null;
        }
        try {
            Object obj = parse.get(str2);
            if (obj == null) {
                return null;
            }
            return (HashMap) obj;
        } catch (Exception unused) {
            return null;
        }
    }

    public boolean parse(int i, String str) {
        if (str == null) {
            return false;
        }
        if (i == 1) {
            HashMap<String, Object> buildModuleMap = buildModuleMap(str);
            this.moduleMap = buildModuleMap;
            if (buildModuleMap == null) {
                this.moduleMap = new HashMap<>();
                return false;
            }
        } else if (i == 2) {
            HashMap<String, Object> buildComMap = buildComMap(str, "Auth");
            this.authMap = buildComMap;
            if (buildComMap == null) {
                this.authMap = new HashMap<>();
                return false;
            }
        } else if (i != 3) {
            return false;
        } else {
            HashMap<String, Object> buildComMap2 = buildComMap(str, "Conf");
            this.confMap = buildComMap2;
            if (buildComMap2 == null) {
                this.confMap = new HashMap<>();
                return false;
            }
            createTicketInfo();
        }
        return true;
    }

    private void createTicketInfo() {
        if (this.authMap == null) {
            Log.info(TAG, "create ticket info fail.");
            return;
        }
        Log.info(TAG, "create ticket info");
        Object obj = get(3, "EMM");
        if (obj instanceof Map) {
            Map map = (Map) obj;
            String str = (String) map.get(TICKET_ENABLE);
            String str2 = (String) map.get(TICKET_LOGIN_CODE);
            if (TextUtils.isEmpty(str) || TextUtils.isEmpty(str2)) {
                Log.warn(TAG, "Config contains no ticket info");
                return;
            }
            Log.info(TAG, "Create Ticket info. Enable:" + str);
            this.mTicketMap.put(IVpnDelegate.KEY_AUTH_TICKETENABLE, str.trim());
            this.mTicketMap.put("auth.ticket.key", str2.trim());
        }
    }

    public String getTicketInfo(String str) {
        Map<String, String> map = this.mTicketMap;
        if (map == null) {
            Log.warn(TAG, "Get ticket info fail.");
            return null;
        }
        return map.get(str);
    }

    public String getString(int i, String str, String str2) {
        Object obj = get(i, str, str2);
        return !(obj instanceof String) ? str2 : (String) obj;
    }

    public Object get(int i, String str, Object obj) {
        Object obj2 = get(i, str);
        return obj2 == null ? obj : obj2;
    }

    public int getVersionType() {
        if (this.mVersionType == 0) {
            String str = (String) get(2, "DeviceType");
            if (str == null || str.isEmpty()) {
                this.mVersionType = 1;
                return 1;
            }
            String lowerCase = str.toLowerCase();
            if (lowerCase.equals("vsp")) {
                this.mVersionType = 2;
            } else if (lowerCase.equals("vdi")) {
                this.mVersionType = 3;
            } else {
                this.mVersionType = 1;
            }
        }
        return this.mVersionType;
    }

    public String getAuthCipher() {
        Object obj = get(2, IVpnDelegate.SSL_CIPHER_SUITE);
        String str = obj instanceof Map ? (String) ((Map) obj).get("EC") : null;
        return (str == null || str.isEmpty()) ? new String("AES128-SHA:RC4-SHA") : str;
    }

    public void setAuthCipher(String str) {
        HashMap hashMap = new HashMap();
        hashMap.put("EC", str);
        this.authMap.put(IVpnDelegate.SSL_CIPHER_SUITE, hashMap);
    }

    public long getGMMode() {
        String str = (String) get(2, IVpnDelegate.GM_VERSION);
        return (str == null || str.equals("") || !str.equals(IGeneral.OPENSSL_GMv1_1)) ? 0L : 1L;
    }

    public String getGMVersion() {
        String str = (String) get(2, IVpnDelegate.GM_VERSION);
        return TextUtils.isEmpty(str) ? IGeneral.OPENSSL_GMv1_0 : str;
    }

    public void setGMOpensslVersion(String str) {
        this.authMap.put(IVpnDelegate.GM_VERSION, str);
    }

    public String getSSLAlogor() {
        String str = (String) get(2, IVpnDelegate.SSL_ALGOR);
        return (str == null || str.equals("")) ? "0" : str;
    }

    public void setSSLAlogor(String str) {
        this.authMap.put(IVpnDelegate.SSL_ALGOR, str);
    }

    public String getLinkCipher(String str) {
        Object obj = get(3, IVpnDelegate.SSL_CIPHER_SUITE);
        if (obj != null && (obj instanceof Map)) {
            return (String) ((Map) obj).get(str);
        }
        return null;
    }

    public String getEnginValue(String str) {
        Object obj = get(3, "SSLEigenvalue");
        if (obj != null && (obj instanceof Map)) {
            return (String) ((Map) obj).get(str);
        }
        return null;
    }

    public String getConfigEFlag() {
        return (String) get(2, CONFIG_TYPE_EFLAG, "");
    }

    public String getMessageDigest() {
        return (String) get(2, "Extra", "");
    }

    public boolean isAntiMITMOpen() {
        String configEFlag = getConfigEFlag();
        if (TextUtils.equals(configEFlag, CONFIG_EFLAG_CLOSE)) {
            return false;
        }
        return (TextUtils.isEmpty(configEFlag) || TextUtils.isEmpty(getMessageDigest()) || TextUtils.equals(configEFlag, CONFIG_EFLAG_CLOSE)) ? false : true;
    }

    public Object get(int i, String str) {
        Object obj;
        if (str == null) {
            return null;
        }
        if (i == 1) {
            HashMap<String, Object> hashMap = this.moduleMap;
            if (hashMap != null) {
                return hashMap.get(str);
            }
            return null;
        } else if (i == 2) {
            HashMap<String, Object> hashMap2 = this.authMap;
            if (hashMap2 != null) {
                return hashMap2.get(str);
            }
            return null;
        } else if (i == 3 && (obj = this.confMap) != null) {
            if (!str.trim().equals("")) {
                String[] split = str.split("\\.");
                for (int i2 = 0; i2 < split.length && (obj instanceof Map) && (obj = ((Map) obj).get(split[i2])) != null; i2++) {
                }
            }
            return obj;
        } else {
            return null;
        }
    }

    public boolean isCrashed() {
        HashMap<String, Object> hashMap = this.moduleMap;
        return hashMap != null && hashMap.size() == 0;
    }

    public String getBugReportUrl() {
        return (String) this.moduleMap.get("BugReport");
    }

    public String getVersion() {
        return (String) this.moduleMap.get("Version");
    }

    public int getVersionInt() {
        String version = getVersion();
        if (this.versionIntMap.containsKey(version)) {
            return this.versionIntMap.get(version).intValue();
        }
        int versionToInt = Common.versionToInt(version);
        this.versionIntMap.put(version, Integer.valueOf(versionToInt));
        return versionToInt;
    }

    public String getSessionId() {
        return GlobalSettingUtils.getTwfid();
    }

    public String getSslctx() {
        String str = (String) get(3, "Other.sslctx");
        Log.debug(IGeneral.LOG_TAG_SETTING, "sslContext:" + str);
        if (str == null || str.trim().length() == 0) {
            return null;
        }
        return str;
    }

    public byte[] getRandomBytes() {
        String str = (String) get(3, "Other.sslctx");
        Log.debug(IGeneral.LOG_TAG_SETTING, "sslContext:" + str);
        if (str == null || str.trim().length() == 0) {
            return null;
        }
        byte[] hexToByte = Crypto.hexToByte(str);
        byte[] bArr = new byte[16];
        System.arraycopy(hexToByte, hexToByte.length - 16, bArr, 0, 16);
        return bArr;
    }

    public String getVpnHost() {
        String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.KEY_L3VPN_HOST);
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str;
    }

    public String getVpnDomain() {
        String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.TWFID_HOST);
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        return str;
    }

    public VpnInfo getVpnInfo() {
        String str = (String) SettingManager.getInstance().get(SettingManager.TWFID_REALLINE);
        if (str == null) {
            return null;
        }
        String trim = str.substring(8).trim();
        if (trim.equals("")) {
            return null;
        }
        String[] split = trim.split(":");
        return new VpnInfo(split[0], split.length > 1 ? Integer.parseInt(split[1]) : IGeneral.DEFAULT_SSL_PORT);
    }

    public boolean isPublicUser() {
        String str = (String) get(3, "Other.isPubUser");
        if (str == null) {
            return false;
        }
        return str.equalsIgnoreCase(ON);
    }

    public long getRetainSessionTime() {
        String str = (String) get(3, "Other.isPubUser");
        String str2 = (String) get(3, "RemoteApp.account_policy");
        String str3 = (String) get(3, "RemoteApp.session_keeptime");
        if (str != null && !str.equals(ON) && str2 != null && !str2.equals(OFF) && str3 != null) {
            try {
                return Long.parseLong(str3);
            } catch (NumberFormatException e) {
                Log.error(TAG, e.getMessage());
            }
        }
        return 0L;
    }

    public boolean getRappReuse() {
        String str = (String) get(3, "RemoteApp.rapp_resuse");
        if (str == null) {
            return true;
        }
        Log.debug(TAG, "rapp_reuse = " + str);
        return str.equals(ON);
    }

    public boolean isClipboardIncomingEnabled() {
        if (getVersionInt() < 5) {
            return false;
        }
        String str = (String) get(3, "RemoteApp.MapClipboard");
        try {
            int parseInt = Integer.parseInt(str);
            return ((parseInt & 1) == 0 || (parseInt & 2) == 0) ? false : true;
        } catch (NumberFormatException unused) {
            Log.warn(TAG, "Invalid clipboard configuration: " + str);
            return false;
        }
    }

    public boolean isL3vpnServiceSupported() {
        return Build.VERSION.SDK_INT >= 14;
    }

    public boolean isClipboardOutgoingEnabled() {
        int versionInt = getVersionInt();
        if (versionInt < 2) {
            return true;
        }
        String str = (String) get(3, "RemoteApp.MapClipboard");
        try {
            int parseInt = Integer.parseInt(str);
            return versionInt < 5 ? (parseInt & 1) != 0 : ((parseInt & 1) == 0 || (parseInt & 4) == 0) ? false : true;
        } catch (NumberFormatException unused) {
            Log.warn(TAG, "Invalid clipboard configuration: " + str);
            return false;
        }
    }

    public boolean isVpnLineEnable() {
        String str = (String) get(3, "Other.sddn_enable", "0");
        Log.error(TAG, "Other.sddn_enable " + str);
        return TextUtils.equals(str, "1");
    }

    /* loaded from: classes2.dex */
    public static class VpnInfo {
        private String host;
        private int port;

        public String getHost() {
            return this.host;
        }

        public int getPort() {
            return this.port;
        }

        public VpnInfo(String str, int i) {
            this.host = str;
            this.port = i;
        }
    }
}
